﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace EasyComm.Client
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }
        private bool bUsernameOK = false;
        private bool bPasswordOK = false;
        private bool bEnableLogin
        {
            get
            {
                if (bUsernameOK && bPasswordOK)
                    return true;
                else
                    return false;
            }
            set
            {
                if (bUsernameOK && bPasswordOK)
                    value = true;
                else
                    value = false;
            }
        }


        public DialogResult LoginDialogResult = DialogResult.Cancel;
        public string LoggedUser = "";
        private void btnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection sqlconn = new SqlConnection();
            sqlconn.ConnectionString = SharedDefines.ConnectionString;
            try
            {
                string szQuery = "select * from login where username = '" + UsernameTxtBox.Text + "'";
                SqlCommand cmd = new SqlCommand(szQuery, sqlconn);
                //MessageBox.Show(szQuery);
                cmd.CommandType = CommandType.Text;
                sqlconn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                bool bLoginSuccess = false;
                while (dr.Read())
                {
                    int pwdOrdinal = dr.GetOrdinal("password");
                    string password = dr.GetString(pwdOrdinal);
                    if (password == PasswordTxtBox.Text)
                    {
                        bLoginSuccess = true;
                        break;
                    }
                }
                if (bLoginSuccess)
                {
                    LoggedUser = UsernameTxtBox.Text;
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("Invalid username or password", "Login failure");
                }
                dr.Close();

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
            finally
            {

                if (sqlconn.State == ConnectionState.Open)
                {
                    sqlconn.Close();
                }
            }
        }

        private void PasswordTxtBox_Validating(object sender, CancelEventArgs e)
        {
            ValidatePasswordField();
        }

        private void UsernameTxtBox_Validating(object sender, CancelEventArgs e)
        {
            ValidateUsernameField();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            if (PasswordTxtBox.Text != "")
                bPasswordOK = true;
            if (UsernameTxtBox.Text != "")
                bUsernameOK = true;
            btnLogin.Enabled = bEnableLogin;
        }

        private void UsernameTxtBox_TextChanged(object sender, EventArgs e)
        {

            ValidateUsernameField();
        }

        private void PasswordTxtBox_TextChanged(object sender, EventArgs e)
        {
            ValidatePasswordField();
        }

        private void ValidateUsernameField()
        {
            if (UsernameTxtBox.Text == "")
            {
                errPUsername.SetError(UsernameTxtBox, "Username cannot be empty");
                bUsernameOK = false;
            }
            else
            {
                errPUsername.SetError(UsernameTxtBox, "");
                bUsernameOK = true;
            }

            btnLogin.Enabled = bEnableLogin;
        }
        private void ValidatePasswordField()
        {
            if (PasswordTxtBox.Text == "")
            {
                errPPassword.SetError(PasswordTxtBox, "Password cannot be empty");
                bPasswordOK = false;
            }
            else
            {
                errPPassword.SetError(PasswordTxtBox, "");
                bPasswordOK = true;
            }
            btnLogin.Enabled = bEnableLogin;
        }

        private void llSignUp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RegisterUserForm Ruf = new RegisterUserForm();
            Ruf.ShowDialog();
        }

        
    }
}
